// Modules to control application life and create native browser window
const {
	app,
	BrowserWindow,
	BrowserView,
	ipcMain,
	Menu,
	Tray
} = require('electron')
require('./server.js')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
let mainLrcWindow
let mainLrcView
let mainLrcWindowPositiona

let tray

function createWindow() {
	// Create the browser window.
	mainWindow = new BrowserWindow({
		//取消安全策略，允许跨域
		webPreferences: {
			webSecurity: false
		},
		width: 1200,
		height: 600,
		title: 'mp3',
		icon: './mp3.png'
	})

	// and load the index.html of the app.
	//	mainWindow.loadFile('./dist/index.html')
	mainWindow.loadURL('http://127.0.0.1:2333')

	// Open the DevTools.
//	mainWindow.webContents.openDevTools()

	const template = [{
			label: '文件',
			submenu: [{
				role: 'open',
				label: '打开',
				click() {
					mainWindow.webContents.send('action', 'open')
				}
			}]
		},
		{
			role: 'window',
			label: '窗口',
			submenu: [{
					role: 'cut',
					label: '剪贴'
				},
				{
					role: 'copy',
					label: '复制'
				},
				{
					role: 'paste',
					label: '粘贴'
				}
			]
		},
		{
			role: 'help',
			label: '休闲一波',
			submenu: [{
				label: '哔哩哔哩',
				click() {
					require('electron').shell.openExternal('https://www.bilibili.com')
				}
			}]
		}
	]

	const menu = Menu.buildFromTemplate(template)
	Menu.setApplicationMenu(menu)

	createSlimeLrc()

	createNotification()

	// Emitted when the window is closed.
	mainWindow.on('closed', function() {
		// Dereference the window object, usually you would store windows
		// in an array if your app supports multi windows, this is the time
		// when you should delete the corresponding element.
		mainWindow = null
		mainLrcWindow.close()
	})
}

function createSlimeLrc() {
	// Create the browser window.
	mainLrcWindow = new BrowserWindow({
		//取消安全策略，允许跨域
		webPreferences: {
			webSecurity: false
		},
		width: 1200,
		height: 160,
		resizable: false,
		transparent: true,
		frame: false,
		alwaysOnTop: true
	})

	mainLrcView = new BrowserWindow({
		width: 300,
		height: 48,
		resizable: false,
		useContentSize: true,
		parent: mainLrcWindow,
		transparent: true,
		frame: false
	})
	mainLrcWindowPosition = mainLrcWindow.getPosition()
	mainLrcView.setPosition(mainLrcWindowPosition[0] + 1200 / 2 - 300 / 2, mainLrcWindowPosition[1] - 48)
	mainLrcView.loadURL('http://127.0.0.1:2333/slimeLrcBtn.html')

	// and load the index.html of the app.
	//	mainWindow.loadFile('./dist/index.html')
	mainLrcWindow.loadURL('http://127.0.0.1:2333/slimeLrc.html')

	//	mainLrcWindow.setIgnoreMouseEvents(true)

	// Open the DevTools.
	//	mainLrcWindow.webContents.openDevTools()

	mainLrcWindow.on('move', function() {
		mainLrcWindowPosition = mainLrcWindow.getPosition()
		mainLrcView.setPosition(mainLrcWindowPosition[0] + 1200 / 2 - 300 / 2, mainLrcWindowPosition[1] - 48)
	})
}

function createNotification() {
	const tray = new Tray('./mp3.png')
	
	const trayMenu = Menu.buildFromTemplate([{
			label: 'Item1',
			type: 'radio'
		},
		{
			label: 'Item2',
			type: 'radio'
		}
	])

	// Make a change to the context menu
	tray.setToolTip('IM云通信')

	// Call this again for Linux because we modified the context menu
	tray.setContextMenu(trayMenu)
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function() {
	// On macOS it is common for applications and their menu bar
	// to stay active until the user quits explicitly with Cmd + Q
	if(process.platform !== 'darwin') {
		app.quit()
	}
})

app.on('activate', function() {
	// On macOS it's common to re-create a window in the app when the
	// dock icon is clicked and there are no other windows open.
	if(mainWindow === null) {
		createWindow()
	}
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.

ipcMain.on('toggleSlimeLrc', (event, arg) => {
	if(arg) {
		mainLrcWindow.show()
		mainLrcView.show()
	} else {
		mainLrcWindow.hide()
		mainLrcView.hide()
	}
})

ipcMain.on('dragSlimeLrc', (event, arg) => {
	mainLrcWindow.setMovable(arg)
	mainLrcView.setMovable(arg)
	mainLrcWindow.setIgnoreMouseEvents(!arg)
})